#include "cxmlconstructor.h"

#include <libxml/encoding.h>
#include <libxml/xmlwriter.h>

#include <string.h>

CXmlConstructor::CXmlConstructor()
{
	_doc = xmlNewDoc(BAD_CAST "1.0");
	_root_node = xmlNewNode(NULL, BAD_CAST "root");
	xmlDocSetRootElement(_doc, _root_node);

	_rt = NULL;
	_fn = NULL;
	_fs = NULL;
}

CXmlConstructor::~CXmlConstructor()
{
}

/*
\brief Adding function declaration
\param fname [in] - function name
\param ftype [in] - function return type
*/
void
CXmlConstructor::AddFunctionDeclaration(const char *fname, const char *ftype)
{
	SetFunctionName(fname);
	SetReturnType  (ftype);

	AddFunctionDeclaration();
}


/*
\brief Adding function declaration
*/
void
CXmlConstructor::AddFunctionDeclaration()
{
	xmlNodePtr n = xmlNewChild(_root_node, NULL, BAD_CAST FUNCTION,BAD_CAST "");
	xmlNewChild(n, NULL, BAD_CAST FUNCTIONNAME, BAD_CAST _fn);
	xmlNewChild(n, NULL, BAD_CAST RETURNTYPE, BAD_CAST _rt);
}


/*
\brief Set type of return value
\param ts [in] - return type
*/
void
CXmlConstructor::SetReturnType(const char *rt)
{
	_rt = rt;	
}

/*
\brief Set type of return value
\param ts [in] - return type
*/
void
CXmlConstructor::SetReturnType(std::string &rt)
{
	int len = rt.length();
	
	delete(_rt);
	char *tmp = new char[len+1];
	tmp[len] = 0;
	rt.copy(tmp, len);
	if(tmp[len-1] == ' ')
		tmp[len-1] = 0;
	_rt = tmp;
}

/*
\brief Set function name
\param fn [in] - return type
*/
void
CXmlConstructor::SetFunctionName(const char *fn)
{
	char *tmp;
	delete(_fn);
	tmp = new char[strlen(fn)+1];
	strcpy(tmp, fn);
	_fn = tmp;
}


#ifdef _DEBUG
void
CXmlConstructor::printTreeToSTDIO()
{
	xmlSaveFormatFileEnc( "-", _doc, "UTF-8", 1);
}
#endif